package org.jeecg.modules.monitoring.j_monitoring.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.monitoring.j_monitoring.entity.MPart;
import org.jeecg.modules.monitoring.j_monitoring.entity.MPartLog;
import org.jeecg.modules.monitoring.j_monitoring.mapper.MPartLogMapper;
import org.jeecg.modules.monitoring.j_monitoring.mapper.MPartMapper;
import org.jeecg.modules.monitoring.j_monitoring.service.IMPartLogService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import jakarta.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * @Description: m_part_log
 * @Author: xiaoyq
 * @Date:   2023-09-11
 * @Version: V1.0
 */
@Service
public class MPartLogServiceImpl extends ServiceImpl<MPartLogMapper, MPartLog> implements IMPartLogService {

    @Resource
    private MPartMapper partMapper;

    public void test(){
        QueryWrapper<MPartLog> queryWrapper = QueryGenerator.initQueryWrapper(new MPartLog(), new HashMap<>());
        List<MPartLog> list = baseMapper.selectList(queryWrapper);

        for (MPartLog log1 : list){
            MPart temp = partMapper.getTemp(log1.getPartId());

            if (temp == null){
                continue;
            }
            MPart part  = partMapper.selectByPartNameAndConfiguration(temp.getPartName(),temp.getConfiguration());

            if (part==null){
                continue;
            }
            log1.setPartId(part.getId());
            log1.setPartName(temp.getPartName());
            log1.setConfiguration(temp.getConfiguration());
            log1.setContractId(part.getContractId());
            log1.setContractName(part.getContractName());


            part.setRemainder(part.getRemainder() - log1.getUseNum());
            part.setUseNum(part.getUseNum() + log1.getUseNum());
            partMapper.updateById(part);

            baseMapper.updateById(log1);
        }

    }
}
